package com.yc.sole.erp.controller;

import com.yc.sole.common.domain.StateCountResp;
import com.yc.sole.erp.biz.OutputBiz;
import com.yc.sole.erp.request.OutputBatchReq;
import com.yc.sole.erp.request.OutputQueryReq;
import com.yc.sole.erp.request.OutputReq;
import com.yc.sole.erp.response.OutputResp;
import com.yc.sole.framework.mybatis.page.BaseBatchIdsReq;
import com.yc.sole.framework.mybatis.page.BasePageReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.DeleteGroup;
import com.yc.sole.framework.validate.SaveGroup;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 班组产量
 *
 * @author yizuomin
 * @date 2024-01-15
 **/
@Validated
@Slf4j
@RestController
@RequiredArgsConstructor
public class OutputController {

    private final OutputBiz outputBiz;

    /**
     * 新增班组产量
     *
     * @param outputReq
     * @return RestResult
     */
    @PreAuth(value = "erp:output:save", description = "新增班组产量")
    @PostMapping("/api/erp/output/saveOutput")
    public Result<Void> saveOutput(@RequestBody @Validated(SaveGroup.class) OutputReq outputReq) {
        outputBiz.saveOutput(outputReq);
        return Result.success();
    }

    /**
     * 修改班组产量
     *
     * @param outputReq
     * @return RestResult
     */
    @PreAuth(value = "erp:output:update", description = "修改班组产量")
    @PostMapping("/api/erp/output/updateOutputById")
    public Result<Void> updateOutputById(@RequestBody @Validated(UpdateGroup.class) OutputReq outputReq) {
        outputBiz.updateOutputById(outputReq);
        return Result.success();
    }

    /**
     * 通过id删除
     *
     * @param baseDeleteReq
     * @return RestResult
     */
    @PreAuth(value = "erp:output:delete", description = "删除班组产量")
    @PostMapping("/api/erp/output/deleteOutput")
    public Result<Void> deleteOutput(@RequestBody @Validated(DeleteGroup.class) BaseBatchIdsReq baseDeleteReq) {
        outputBiz.deleteOutput(baseDeleteReq.getIds());
        return Result.success();
    }

    /**
     * 通过id查询班组产量
     *
     * @param id
     * @return RestResult
     */
    @PreAuth(value = "erp:output:select", description = "通过id查询班组产量")
    @PostMapping("/api/erp/output/selectOutputById/{id}")
    public Result<OutputResp> selectOutputById(@PathVariable Long id) {
        return Result.data(outputBiz.selectOutputById(id));
    }

    /**
     * 分页查询
     *
     * @param basePageReq
     * @return Result
     */
    @PreAuth(value = "erp:output:select", description = "查询班组产量")
    @PostMapping("/api/erp/output/selectOutputPage")
    public Result<Paging<OutputResp>> selectOutputPage(@RequestBody @Validated BasePageReq<OutputQueryReq> basePageReq) {
        return Result.data(outputBiz.selectOutputPage(basePageReq.toPage(), basePageReq.getQuery()));
    }

    /**
     * 根据日期和班组id查询班组产量
     *
     * @param outputQueryReq
     * @return Result
     */
    @PreAuth(value = "erp:output:select", description = "根据日期和班组id查询班组产量")
    @PostMapping("/api/erp/output/selectByShiftIdAndOutputDate")
    public Result<Integer> selectByShiftIdAndOutputDate(@RequestBody OutputQueryReq outputQueryReq) {
        return Result.data(outputBiz.selectByShiftIdAndOutputDate(outputQueryReq.getShiftId(), outputQueryReq.getOutputDate()));
    }

    /**
     * 查询班次数量
     *
     * @return RestResult
     */
    @PreAuth(value = "erp:output:select", description = "查询班次数量")
    @PostMapping("/api/erp/output/selectShiftCount")
    public Result<List<StateCountResp>> selectShiftCount(@RequestBody OutputQueryReq queryReq) {
        return Result.data(outputBiz.selectShiftCount(queryReq));
    }

    /**
     * 查询当前查询条件的总产量
     *
     * @return RestResult
     */
    @PreAuth(value = "erp:output:select", description = "查询当前查询条件的总产量")
    @PostMapping("/api/erp/output/selectOutputQuantity")
    public Result<List<StateCountResp>> selectOutputQuantity(@RequestBody OutputQueryReq queryReq) {
        return Result.data(outputBiz.selectOutputQuantity(queryReq));
    }

    /**
     * ocr识别文件内容
     *
     * @param file
     * @return
     */
    @PreAuth(value = "erp:output:update", description = "ocr识别文件内容")
    @PostMapping("/api/erp/output/ocr")
    public Result<List<OutputResp>> ocr(@NotNull(message = "上传的图片不能为空") @RequestParam("file") MultipartFile file) {
        return Result.data(outputBiz.ocr(file));
    }

    /**
     * 批量保存产量登记
     *
     * @param batchReq
     * @return
     */
    @PreAuth(value = "erp:output:update", description = "批量保存产量登记")
    @PostMapping("/api/erp/output/saveOutputBatch")
    public Result<Void> saveOutputBatch(@RequestBody @Validated(SaveGroup.class) OutputBatchReq batchReq) {
        outputBiz.saveOutputBatch(batchReq);
        return Result.success();
    }
}
